package cn.stylefeng.guns.sys.modular.system.db4.service.impl;


import cn.stylefeng.guns.sys.modular.system.db4.entity.Team;
import cn.stylefeng.guns.sys.modular.system.db4.mapper.TeamMapper;
import cn.stylefeng.guns.sys.modular.system.db4.model.params.TeamParam;
import cn.stylefeng.guns.sys.modular.system.db4.model.result.TeamResult;
import cn.stylefeng.guns.sys.modular.system.db4.service.TeamService;
import cn.stylefeng.guns.sys.modular.system.model.result.YyjlResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 团队表 服务实现类
 * </p>
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {

    @Resource
    private TeamMapper teamMapper;


    @Override
    public List<TeamResult> findListBySpec(TeamParam param) {
        return teamMapper.findListBySpec(param);
    }

    /**
     * 查询营运经理列表
     * @return
     */
    @Override
    public List<Map> selectYyjlList() {
        List<Map> list = teamMapper.selectYyjlList();
        return list;
    }

    @Override
    public List<YyjlResult> selectYyjlLists() {
        List<YyjlResult> list = teamMapper.selectYyjlLists();
        return list;
    }

    /**
     * 钉钉同步手机号
     *
     * @param oldTels,mobile
     * @return
     */
    @Override
    public void updateTeamMobile(String oldTels,String mobile) {
        //团队培训表中查找是否有符合的手机号
        List<Team> teamList=new LambdaQueryChainWrapper<>(teamMapper)
                .eq(Team::getMobile,oldTels)
                .list();
        //根据已查到的手机号匹配培训表手机号 匹配到先更新保存原手机号，然后替换新手机号
        teamList.forEach(i -> {
            if(!i.getMobile().equals(mobile)){
                LambdaQueryWrapper<Team> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Team::getMobile,mobile);
                Team teamOne = teamMapper.selectOne(queryWrapper);
                if(teamOne==null){
                    i.setOldTels(i.getMobile());
                    i.setMobile(mobile);
                    //引入团队培训表数据操作层
                    teamMapper.updateById(i);
                }
            }
        });
    }

    /**
     * 查询督导列表
     * @param phone
     * @return
     */
    @Override
    public List<Map> selectDdList(String phone) {
        return teamMapper.selectDdList(phone);
    }
    /**
     * 查询督导列表--全部
     * @return
     */
    @Override
    public List<Map> selectDdListAll() {
        return teamMapper.selectDdListAll();
    }


}
